package sortingRMI_server;

import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

import java.rmi.RemoteException;

import org.junit.Before;
import org.junit.Test;

import sortingRMI_client.ISorter;
import sortingRMI_client.PreSortChecker;


public class TestQuicksort {
	
	Comparable[] testInputArray;
	PreSortChecker PreSort = new PreSortChecker();

	@Before
	public void setUp() throws Exception {
		
		final int testInputArraySize = 20;
		
		
		
		testInputArray = new Comparable[testInputArraySize];
		
		for (int i = 0; i < testInputArraySize; i++) {
			testInputArray[i] = (int) Math.random() * testInputArraySize + 1;
		}
		
	}

	// test null array to be sorted
	@Test
	(expected = IllegalArgumentException.class) public void testSortNull()  throws RemoteException{
		Comparable[] thisShouldBeNull;
		
		ISorter mySortTest = new Quicksort();
		
		thisShouldBeNull = mySortTest.sort(null);
	}

	// test empty array to be sorted
	@Test
	(expected = IllegalArgumentException.class) public void testSortEmpty() throws RemoteException {
		Comparable[] thisShouldBeEmpty;
		Comparable[] thisShouldBeNull;
		
		ISorter mySortTest = new Quicksort();		
		
		thisShouldBeNull = mySortTest.sort(new Comparable[0]);
	}
	
	// test sorter returns object that has the same length as the input object
	@Test
	public void testSortOutputIsSameLengthAsInput() throws RemoteException {
		int inputLength;
		int outputLenght;
		Comparable[] testOutputArray;
		
		ISorter mySortTest = new Quicksort();	
		
		inputLength = testInputArray.length;
				
		testOutputArray = mySortTest.sort(testInputArray);
		
		assertTrue(inputLength == testOutputArray.length);
	}
	
	// test sorter actually works! maybe! (3 tests)
	@Test
	public void testSortIntWorks() throws RemoteException {
		Comparable[] testIntArray = { 9 , 8 };

		Comparable[] testSortedArray;

		ISorter mySortTest = new Quicksort();	
				
		testSortedArray = mySortTest.sort(testIntArray);
		assertTrue(testSortedArray[0].equals(8));
	}
	
	@Test
	public void testSortCharWorks() throws RemoteException {
		Comparable[] testCharArray = { "b", "a" };
	
		Comparable[] testSortedArray;
	
		ISorter mySortTest = new Quicksort();	
	
		testSortedArray = mySortTest.sort(testCharArray);
		assertTrue(testSortedArray[0].equals("a"));
	}
	
	@Test
	public void testSortStringWorks() throws RemoteException {
		Comparable[] testStringArray = { "foo", "bar" };
	
		Comparable[] testSortedArray;
	
		ISorter mySortTest = new Quicksort();	
	
		testSortedArray = mySortTest.sort(testStringArray);
		assertTrue(testSortedArray[0].equals("bar"));
	}
	
	// test for pre-sorted arrays (4 tests)
	@Test
	public void testPresortedIntArray() throws RemoteException {
		Comparable[] testPresortedIntArray = { 1,2,3,4,5,6,7,8,9 };
	
		ISorter mySortTest = new Quicksort();	
	
		assertTrue(PreSort.checkSort(testPresortedIntArray));
	}
	
	@Test
	public void testNotPresortedIntArray() {
		Comparable[] testNotPresortedIntArray = { 9,2,3,4,5,6,7,8,9 };
	
		assertFalse(PreSort.checkSort(testNotPresortedIntArray));
	}
	
	@Test
	public void testPresortedCharArray() {
		Comparable[] testPresortedCharArray = { "a","b","c","d","e","f" };
	
		assertTrue(PreSort.checkSort(testPresortedCharArray));
	}
	
	@Test
	public void testNotPresortedCharArray() {
		Comparable[] testNotPresortedCharArray = { "a","f","c","d","e","f" };
	
		assertFalse(PreSort.checkSort(testNotPresortedCharArray));
	}
}